{Hint: save all files to location: \jni }
unit CstmModule;

{$mode delphi}

interface

uses
  Classes, SysUtils, AndroidWidget, Laz_And_Controls, preferences, expandablelistview, lNet, fpjson, ucommon, ubase64;
  
type

  { TCstmForm }

  TCstmForm = class(jForm)
    jBtnDetail: jButton;
    jEditText1: jEditText;
    jEditText2: jEditText;
    jEditText3: jEditText;
    jEditText4: jEditText;
    jEditText5: jEditText;
    jEditText6: jEditText;
    jEditText7: jEditText;
    jEditText8: jEditText;
    jEditText0: jEditText;
    jEdtCustomer: jEditText;
    jLsvCustomer: jExpandableListView;
    jPanCust: jPanel;
    jPreferens: jPreferences;
    jPanHeader: jPanel;
    jVewCont: jScrollView;
    jTextView3: jTextView;
    procedure CstmFormCloseQuery(Sender: TObject; var CanClose: boolean);
    procedure CstmFormJNIPrompt(Sender: TObject);
    procedure CstmFormSpecialKeyDown(Sender: TObject; keyChar: char; keyCode: integer; keyCodeString: string; var mute: boolean);
    procedure jBtnDetailClick(Sender: TObject);
    procedure jLsvCustomerGroupItemCollapse(Sender: TObject; groupItemPosition: integer; groupItemHeader: string);
    procedure jLsvCustomerGroupItemExpand(Sender: TObject; groupItemPosition: integer; groupItemHeader: string);
    procedure jLsvCustomerOnChildItemClick(Sender: TObject; groupItemPosition: integer; groupItemHeader: string; childItemPosition: integer; childItemCaption: string);
    procedure OnLTcpReceived(aSocket: TLSocket);
    procedure OnLTcpError(const msg: string; aSocket: TLSocket);
  private
    {private declarations}
  public
    lConn: TLTCP;
  end;

var
  CstmForm: TCstmForm;
  rcv : string;
  JSONCustomer: TJSONData;
  FormCanClose: Boolean = True;
implementation
  
{$R *.lfm}
  

{ TCstmForm }

procedure TCstmForm.CstmFormJNIPrompt(Sender: TObject);
var
s, es: string;
b : pbyte;
i, n: dword;
jsonObj, jsParam: TJSONObject;
begin
  jEdtCustomer.Text := jPreferens.GetStringData('CUSTOMER','');
  lConn:= TLTCP(gPtrConn^);
  lConn.OnReceive := OnLTcpReceived;
  lConn.OnError   := OnLTcpError;
  rcv := '';
  jPanCust.MatchParent();
  //jVewCont.MatchParent();
  jVewCont.LayoutParamHeight := lpMatchParent;
  // 准备参数
  jsonObj := TJSONObject.Create;
  jsParam := TJSONObject.Create;
  jsonObj.Add('TIME',Now());
  jsonObj.Add('CMD','GetCustomer');
  jsonObj.Add('PARM', jsParam);
  // 加密
  s := jsonObj.AsJSON;
  es:= TFun.Encrypt(s);
  // BASE64
  i := length(es);
  n := (i div 3) * 4 + 5;
  Getmemory(b, n);
  FillByte(b^, n, 0);
  Base64Encode(PChar(es), i, b, n);
  if lConn.connected then
    lConn.Send(b^,n);
  Freemem(b);
end;

procedure TCstmForm.CstmFormSpecialKeyDown(Sender: TObject; keyChar: char; keyCode: integer; keyCodeString: string; var mute: boolean);
begin
  //ShowMessage('keyCode:'+inttostr(keyCode) +'  keyCodeString=' + keyCodeString);
  if 'KEYCODE_BACK'=keyCodeString then
  begin
    FormCanClose := jPanCust.Visible;
    jPanCust.Visible := True;
    jVewCont.Visible := False;
  end;
end;


procedure TCstmForm.CstmFormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
  CanClose := FormCanClose;
end;


procedure TCstmForm.jBtnDetailClick(Sender: TObject);
var
  s, es: string;
  b : pbyte;
  i, n: dword;
  jsonObj, jsParam: TJSONObject;
begin
  if Trim(jEdtCustomer.Text)='' then Exit;
  jVewCont.Visible:=true;
  jPanCust.Visible:=False;

  if Trim(jEdtCustomer.Text)='' then Exit;
  // 准备参数
  jsonObj := TJSONObject.Create;
  jsParam := TJSONObject.Create;
  jsonObj.Add('TIME',Now());
  jsonObj.Add('CMD','GetCustInfo');
  jsonObj.Add('PARM', jsParam);
  jsParam.Add('PMA',jEdtCustomer.Text);
  // 加密
  s := jsonObj.AsJSON;
  es:= TFun.Encrypt(s);
  // BASE64
  i := length(es);
  n := (i div 3) * 4 + 5;
  Getmemory(b, n);
  FillByte(b^, n, 0);
  Base64Encode(PChar(es), i, b, n);
  if lConn.connected then
  begin
    rcv := '';
    lConn.Send(b^,n);
  end;
    Freemem(b);
end;

procedure TCstmForm.jLsvCustomerGroupItemCollapse(Sender: TObject; groupItemPosition: integer; groupItemHeader: string);
begin
  jEdtCustomer.Text := groupItemHeader;
end;

procedure TCstmForm.jLsvCustomerGroupItemExpand(Sender: TObject; groupItemPosition: integer; groupItemHeader: string);
begin
  jEdtCustomer.Text := groupItemHeader;
end;

procedure TCstmForm.jLsvCustomerOnChildItemClick(Sender: TObject; groupItemPosition: integer; groupItemHeader: string; childItemPosition: integer;
  childItemCaption: string);
begin
  jBtnDetailClick(Sender);
end;


procedure TCstmForm.OnLTcpReceived(aSocket: TLSocket);
var
  s,ts,str: string;
  i,len: integer;
  j: TJSONObject;
  d: TJSONData;
  a: TJSONArray;
begin
  //接收信息
  lConn.GetMessage(s);
  rcv := rcv + s;
  len := Length(s);
  ts := Copy(s, len-4, 5);
  if ('$CUS#'=ts) then
  begin
    rcv := Copy(rcv, 0, Length(rcv)-5);
    if AsSigned(JSONCustomer) then
      JSONCustomer.Clear;
    JSONCustomer := GetJSON(TFun.UnzipString(rcv));
    a := TJSONArray(JSONCustomer.FindPath('T'));
    rcv := '';
    //jLsvCustomer.Clear;
    jPanCust.Visible:=true;
    jVewCont.Visible:=False;
    for i:=0 to a.Count -1 do
    begin
      j := a.Objects[i];
      jLsvCustomer.Add(j.Strings['CUSTOMER'], j.Strings['ADDRESS']+';'+j.Strings['MEMO']);
    end;
  end else
  if ('$CIF#'=ts) then
  begin
    rcv := Copy(rcv, 0, Length(rcv)-5);
    d := GetJSON(TFun.UnzipString(rcv));
    j := TJSONObject(d);
    rcv := '';
    //jLsvContent.Clear;
    jVewCont.Visible:=true;
    jPanCust.Visible:=False;
    jEditText0.Clear;
    jEditText0.AppendLn('TEL: ' + j.Strings['TEL']);
    jEditText0.AppendLn('FAX: ' + j.Strings['FAX']);
    // 1
    jEditText1.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK1TYPE'],j.Strings['LINK1NAME'],j.Strings['LINK1TEL'],j.Strings['LINK1TELEXT']]);
    jEditText1.AppendLn(str);
    if Trim(j.Strings['LINK1MOBILE'])<>'' then jEditText1.AppendLn('  ' + j.Strings['LINK1MOBILE']);
    if Trim(j.Strings['LINK1EMAIL'])<>'' then jEditText1.AppendLn('  ' + j.Strings['LINK1EMAIL']);
    if Trim(j.Strings['LINK1MEMO'])<>'' then jEditText1.AppendLn('  ' + j.Strings['LINK1MEMO']);
    // 2
    jEditText2.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK2TYPE'],j.Strings['LINK2NAME'],j.Strings['LINK2TEL'],j.Strings['LINK2TELEXT']]);
    jEditText2.AppendLn(str);
    if Trim(j.Strings['LINK2MOBILE'])<>'' then jEditText2.AppendLn('  ' + j.Strings['LINK2MOBILE']);
    if Trim(j.Strings['LINK2EMAIL'])<>'' then jEditText2.AppendLn('  ' + j.Strings['LINK2EMAIL']);
    if Trim(j.Strings['LINK2MEMO'])<>'' then jEditText2.AppendLn('  ' + j.Strings['LINK2MEMO']);
    // 3
    jEditText3.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK3TYPE'],j.Strings['LINK3NAME'],j.Strings['LINK3TEL'],j.Strings['LINK3TELEXT']]);
    jEditText3.AppendLn(str);
    if Trim(j.Strings['LINK3MOBILE'])<>'' then jEditText3.AppendLn('  ' + j.Strings['LINK3MOBILE']);
    if Trim(j.Strings['LINK3EMAIL'])<>'' then jEditText3.AppendLn('  ' + j.Strings['LINK3EMAIL']);
    if Trim(j.Strings['LINK3MEMO'])<>'' then jEditText3.AppendLn('  ' + j.Strings['LINK3MEMO']);
    // 4
    jEditText4.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK4TYPE'],j.Strings['LINK4NAME'],j.Strings['LINK4TEL'],j.Strings['LINK4TELEXT']]);
    jEditText4.AppendLn(str);
    if Trim(j.Strings['LINK4MOBILE'])<>'' then jEditText4.AppendLn('  ' + j.Strings['LINK4MOBILE']);
    if Trim(j.Strings['LINK4EMAIL'])<>'' then jEditText4.AppendLn('  ' + j.Strings['LINK4EMAIL']);
    if Trim(j.Strings['LINK4MEMO'])<>'' then jEditText4.AppendLn('  ' + j.Strings['LINK4MEMO']);
    // 5
    jEditText5.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK5TYPE'],j.Strings['LINK5NAME'],j.Strings['LINK5TEL'],j.Strings['LINK5TELEXT']]);
    jEditText5.AppendLn(str);
    if Trim(j.Strings['LINK5MOBILE'])<>'' then jEditText5.AppendLn('  ' + j.Strings['LINK5MOBILE']);
    if Trim(j.Strings['LINK5EMAIL'])<>'' then jEditText5.AppendLn('  ' + j.Strings['LINK5EMAIL']);
    if Trim(j.Strings['LINK5MEMO'])<>'' then jEditText5.AppendLn('  ' + j.Strings['LINK5MEMO']);
    // 6
    jEditText6.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK6TYPE'],j.Strings['LINK6NAME'],j.Strings['LINK6TEL'],j.Strings['LINK6TELEXT']]);
    jEditText6.AppendLn(str);
    if Trim(j.Strings['LINK6MOBILE'])<>'' then jEditText6.AppendLn('  ' + j.Strings['LINK6MOBILE']);
    if Trim(j.Strings['LINK6EMAIL'])<>'' then jEditText6.AppendLn('  ' + j.Strings['LINK6EMAIL']);
    if Trim(j.Strings['LINK6MEMO'])<>'' then jEditText6.AppendLn('  ' + j.Strings['LINK6MEMO']);
    // 7
    jEditText7.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK7TYPE'],j.Strings['LINK7NAME'],j.Strings['LINK7TEL'],j.Strings['LINK7TELEXT']]);
    jEditText7.AppendLn(str);
    if Trim(j.Strings['LINK7MOBILE'])<>'' then jEditText7.AppendLn('  ' + j.Strings['LINK7MOBILE']);
    if Trim(j.Strings['LINK7EMAIL'])<>'' then jEditText7.AppendLn('  ' + j.Strings['LINK7EMAIL']);
    if Trim(j.Strings['LINK7MEMO'])<>'' then jEditText7.AppendLn('  ' + j.Strings['LINK7MEMO']);
    // 8
    jEditText8.Clear;
    str := format('%s  %s  %s  %s', [j.Strings['LINK8TYPE'],j.Strings['LINK8NAME'],j.Strings['LINK8TEL'],j.Strings['LINK8TELEXT']]);
    jEditText8.AppendLn(str);
    if Trim(j.Strings['LINK8MOBILE'])<>'' then jEditText8.AppendLn('  ' + j.Strings['LINK8MOBILE']);
    if Trim(j.Strings['LINK8EMAIL'])<>'' then jEditText8.AppendLn('  ' + j.Strings['LINK8EMAIL']);
    if Trim(j.Strings['LINK8MEMO'])<>'' then jEditText8.AppendLn('  ' + j.Strings['LINK8MEMO']);
  end;
end;

procedure TCstmForm.OnLTcpError(const msg: string; aSocket: TLSocket);
begin
 ShowMessage(msg);
end;

end.
